package servlet;

import dao.OrderDao;
import dao.OrderItemDao;
import dao.factoty.DaoFactory;
import util.DataSourceUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;

@WebServlet(name = "DelOrderByIdServlet", value = "/DelOrderByIdServlet")
public class DelOrderByIdServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("id");
        OrderItemDao orderItemDao = DaoFactory.getOrderItemDaoInstance();
        OrderDao orderDao = DaoFactory.getOrderDAOInstance();
        Connection connection = null;
        try {
            connection = DataSourceUtils.getDataSource().getConnection();
            connection.setAutoCommit(false);
            orderDao.removeOrderById(id);
            orderItemDao.removeOrderItemById(id);
            connection.commit();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        request.getRequestDispatcher("/FindOrdersServlet").forward(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        super.doPost(request, response);
    }
}
